package hongcaosp.app.android.video;

import android.content.Context;
import android.os.Environment;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.tencent.liteav.demo.common.utils.FileUtils;
import com.tencent.liteav.demo.common.utils.HttpFileListener;
import com.tencent.liteav.demo.common.utils.HttpFileUtil;

import java.io.File;
import java.util.Date;

import hongcaosp.app.android.modle.bean.IVideo;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.AlbumNotifyHelper;
import xlj.lib.android.base.utils.TaskPool;

public class VideoDownload {
    private Context context;
    private KProgressHUD progressHUD;
    private IVideo video;

    public VideoDownload(Context context, IVideo video) {
        this.context = context;
        this.video = video;
    }

    public void start() {
        String url = video.getVideoUrl();
        String tempStr = url.substring(url.lastIndexOf("/") + 1);
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/redgrass/txvodcache", tempStr);
        if (file.exists()) {
            AlbumNotifyHelper.scanFile(context,file.getAbsolutePath());
        } else {
            progressHUD = KProgressHUD.create(context).setStyle(KProgressHUD.Style.ANNULAR_DETERMINATE);
            progressHUD.setMaxProgress(100);
            progressHUD.show();
            HttpFileUtil httpFileUtil = new HttpFileUtil(context, url, Environment.getExternalStorageDirectory().getAbsolutePath() + "/redgrass/txvodcache",
                    tempStr, new HttpFileListener() {
                @Override
                public void onProgressUpdate(final int progress) {
                    TaskPool.myPool().postTaskOnMain(new Runnable() {
                        @Override
                        public void run() {
                            progressHUD.setProgress(progress);
                        }
                    });
                }

                @Override
                public void onSaveSuccess(final File file) {
                    TaskPool.myPool().postTaskOnMain(new Runnable() {
                        @Override
                        public void run() {
                            progressHUD.dismiss();
                            AlbumNotifyHelper.insertVideoToMediaStore(context,file.getAbsolutePath(),new Date().getTime(),video.getVideoSeconds() * 1000);
                            ToastManager.getInstance().showToast("下载成功到：" + file.getAbsolutePath(), 5000);
                        }
                    });
                }

                @Override
                public void onSaveFailed(File file, Exception e) {
                    TaskPool.myPool().postTaskOnMain(new Runnable() {
                        @Override
                        public void run() {
                            progressHUD.dismiss();
                            ToastManager.getInstance().showToast("下载失败");
                        }
                    });
                }

                @Override
                public void onProcessEnd() {

                }
            }, true);
            TaskPool.myPool().postBg(httpFileUtil);
        }
    }
}
